package org.nobody.mallb2c.mallware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.nobody.common.exception.NoStockException;
import org.nobody.common.exception.ResultCodeEnum;
import org.nobody.mallb2c.mallware.vo.LockStockResult;
import org.nobody.mallb2c.mallware.vo.SkuHasStockVo;
import org.nobody.mallb2c.mallware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.nobody.mallb2c.mallware.entity.WareSkuEntity;
import org.nobody.mallb2c.mallware.service.WareSkuService;
import org.nobody.common.utils.PageUtils;
import org.nobody.common.utils.R;


/**
 * 商品库存
 *
 * @author fancier
 * @email fancier2093@gmail.com
 * @date 2020-07-30 15:47:39
 */
@RestController
@RequestMapping("mallware/waresku")
public class WareSkuController {
  @Autowired
  private WareSkuService wareSkuService;

  @PostMapping("/lock/order")
  public R orderLockStock(@RequestBody WareSkuLockVo vo){
    try {
      wareSkuService.orderLockStock(vo);
      return R.ok();
    } catch (NoStockException e) {
      e.printStackTrace();
      return R.error(ResultCodeEnum.NO_STOCK_EXCEPTION.getCode(), ResultCodeEnum.NO_STOCK_EXCEPTION.getMsg());
    }
  }

  //查询sku是否有库存
  @PostMapping("/hasstock")
  public R getSkusHasStock(@RequestBody List<Long> skuIds) {
    //返回 sku_id,stock
    List<SkuHasStockVo> vos = wareSkuService.getSkusHasStock(skuIds);

    return R.ok().setData(vos);
  }

  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("mallware:waresku:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = wareSkuService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  //@RequiresPermissions("mallware:waresku:info")
  public R info(@PathVariable("id") Long id) {
    WareSkuEntity wareSku = wareSkuService.getById(id);

    return R.ok().put("wareSku", wareSku);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("mallware:waresku:save")
  public R save(@RequestBody WareSkuEntity wareSku) {
    wareSkuService.save(wareSku);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("mallware:waresku:update")
  public R update(@RequestBody WareSkuEntity wareSku) {
    wareSkuService.updateById(wareSku);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("mallware:waresku:delete")
  public R delete(@RequestBody Long[] ids) {
    wareSkuService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
